#include "DecodeDSF.h"
#include "DecodeDSDIFF.h"
#include "EncodeDOP.h"

int main(int argc, char** argv)
{
    DecodeDSDFile* decoder;
    ASSERT(argc == 2, "Usage: dsd2dop <file_name>");
    std::string file_name = argv[1];
    if (file_name.find(".dsf") != std::string::npos)
	decoder = new DecodeDSF;
    else if (file_name.find(".dff") != std::string::npos)
	decoder = new DecodeDSDIFF;
    else
	DIE("Unknown file extension " << file_name);

    decoder->open(file_name);
    EncodeDOP encoder;

#define BUF_SIZE (4096 * 2)
    uint8_t buf[BUF_SIZE];
    int did_read;
    uint8_t encoded_buf[BUF_SIZE / 2 * 3];
    
    while ( (did_read = decoder->read(buf, sizeof(buf))) > 0)
    {
	did_read -= did_read % 4;
	encoder.encode(buf, did_read, encoded_buf);
	ASSERT(write(fileno(stdout), encoded_buf, did_read / 2 * 3) == did_read / 2 * 3, "error writing to stdout");
    }
    delete decoder;
}

